<template>
  <div class="order-container">
    <!-- 订单列表表格 -->
    <el-table :data="orderList" border style="width: 100%">
      <el-table-column prop="orderNo" label="订单编号"></el-table-column>
      <el-table-column prop="cjTime" label="创建时间" width="180"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称"></el-table-column>
      <el-table-column prop="price" label="金额/积分"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click="showDetail(scope.row.id)" type="text">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curPage"
      :page-sizes="[10, 20, 50]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 详情弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="订单详情" width="50%">
      <div v-if="currentOrder">
        <el-form >
          <el-form-item label="订单编号">{{ currentOrder.orderNo }}</el-form-item>
          <el-form-item label="支付方式">
            {{ payTypeMap[currentOrder.payType] }}
          </el-form-item>
          <el-form-item label="订单状态">
            {{ orderStateMap[currentOrder.orderState] }}
          </el-form-item>
          <el-form-item label="实付金额">{{ currentOrder.price }}</el-form-item>
          <el-form-item label="收货地址">{{ currentOrder.deliveryAddress }}</el-form-item>
          <el-form-item label="收件人">{{ currentOrder.recipient }}</el-form-item>
          <el-form-item label="联系电话">{{ currentOrder.mobile }}</el-form-item>
          <el-form-item label="商品数量">{{ currentOrder.goodsNum }}</el-form-item>
          <el-form-item label="商品封面">
            <el-image 
              style="width: 100px; height: 100px"
              :src="currentOrder.goodsCoverPic"
              :preview-src-list="[currentOrder.goodsCoverPic]">
            </el-image>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      curPage: 1,
      limit: 10,
      total: 0,
      orderList: [],
      dialogVisible: false,
      currentOrder: null,
      payTypeMap: {
        1: '微信支付',
        2: '支付宝',
        3: '积分支付'
      },
      orderStateMap: {
        0: '待支付',
        1: '已支付',
        2: '已发货',
        3: '已完成',
        4: '已取消'
      }
    };
  },
  created() {
    this.fetchOrderList();
  },
  methods: {
    // 获取订单列表
     fetchOrderList() {
      this.$http({
          url: this.$http.adornUrl("yshbms/order/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.curPage,
            limit: this.limit
          }),
        }).then(({ data }) => {
          console.log(data,'data');
          if (data && data.code === 0) {
            this.orderList = data.page;
            this.total = data.total;
          } else {
            this.orderList = [];
            this.total = 0;
          }
        });
    },

    // 显示详情弹窗
     showDetail(id) {
      this.dialogVisible = true
      this.$http({
            url: this.$http.adornUrl(
              `yshbms/order/info/${id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.currentOrder = data.order2Vo;
              console.log(this.dataForm);
            } else {
        this.$message.error('获取订单详情失败');
            }
          });
    },

    // 分页处理
    handleSizeChange(val) {
      this.limit = val;
      this.fetchOrderList();
    },
    handleCurrentChange(val) {
      this.curPage = val;
      this.fetchOrderList();
    }
  }
};
</script>

<style scoped>
.order-container {
  padding: 20px;
}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
